package com.bnz.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.bnz.manager.dao.SpecificationDao;
import com.bnz.manager.dao.SpecificationOptionDao;
import com.bnz.manager.entity.SpecificationOptionEntity;
import com.bnz.manager.entity.group.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.TypeTemplateDao;
import com.bnz.manager.entity.TypeTemplateEntity;
import com.bnz.manager.service.TypeTemplateService;


@Service("typeTemplateService")
public class TypeTemplateServiceImpl extends ServiceImpl<TypeTemplateDao, TypeTemplateEntity> implements TypeTemplateService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SpecificationOptionDao optionDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TypeTemplateEntity> page = this.page(
                new Query<TypeTemplateEntity>().getPage(params),
                new QueryWrapper<TypeTemplateEntity>()
        );

        return new PageUtils(page);
    }

    //一模板id为key以平拍列表为值放到redis中
    //以模板id为key 以规格列表为值放到redis中
    @Override
    public TypeTemplateEntity findById(String id) {
        //查询出模板对象
        TypeTemplateEntity temp = this.getById(id);
        return temp;
    }

    @Override
    public List<TypeTemplateEntity> findAll() {
        //查询所有模板
        List<TypeTemplateEntity> list = this.list();
        for (TypeTemplateEntity temp : list) {
            //查询出平排列表
            String brandIds = temp.getBrandIds();
            //将平拍列表转换出list的集合
            List<Map>  brandList = JSON.parseArray(brandIds,Map.class);
            //将转化后的 品牌列表放到redis中  --
            BoundHashOperations<String, String, String> brandList1 = redisTemplate.boundHashOps("brandList");
            brandList1.put(temp.getId(),JSON.toJSONString(brandList));

            //查询规格列表
            String specIds = temp.getSpecIds();
            //转换橙list集合放到到redis中
            List<Map> specMap = JSON.parseArray(specIds,Map.class);
            //便利后放入redis
            for (Map map : specMap) {
                //得到规格id 转换字符串
                String specId = map.get("id")+"";
                //在规格表中根据他的id查询出他的规格  -- 的得到规格
                List<SpecificationOptionEntity> options = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
                //将得到的规格放到map集合中
                map.put("options",options);
            }
            //将规格和规格选项放到redis中
            //   使用map
            BoundHashOperations<String, String, String> specList = redisTemplate.boundHashOps("specList");
            specList.put(temp.getId(),JSON.toJSONString(specMap));
        }
        return list;
    }

}